using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Commonplace.Domain
{
    public class WikiConnectionConfig
    {
        private string _name;
        private string _hostname;
        private string _basePath;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Hostname
        {
            get { return _hostname; }
            set { _hostname = value; }
        }

        public string BasePath
        {
            get { return _basePath; }
            set { _basePath = value; }
        }

        /// <summary>
        /// Creates a default commons connection
        /// </summary>
        public WikiConnectionConfig()
        {
            Name = "Wikimedia Commons";
            Hostname = "commons.wikimedia.org";
            BasePath = "/w/index.php";
        }


        public WikiConnectionConfig(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlElement nameElement = doc.DocumentElement;
            XmlElement hostnameElement = doc.SelectSingleNode("//hostname") as XmlElement;
            XmlElement basePathElement = doc.SelectSingleNode("//basepath") as XmlElement;

            _name = nameElement.GetAttribute("name");
            _hostname = hostnameElement.InnerText;
            _basePath = basePathElement.InnerText;
        }

        public override string ToString()
        {
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateElement("wiki"));
            XmlElement hostnameElement = doc.CreateElement("hostname");
            XmlElement basePathElement = doc.CreateElement("basepath");

            doc.DocumentElement.SetAttribute("name", Name);
            hostnameElement.InnerText = Hostname;
            basePathElement.InnerText = BasePath;
            doc.DocumentElement.AppendChild(hostnameElement);
            doc.DocumentElement.AppendChild(basePathElement);

            return doc.OuterXml;
        }
            
    }
}
